package com.bstek.bdf4.investigate.service;

import com.bstek.bdf4.investigate.dao.CategoryDao;
import com.bstek.bdf4.investigate.dao.CategoryJPADao;
import com.bstek.bdf4.investigate.entity.Category;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

@Component
public class CategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    CategoryJPADao categoryJPADao;
    // 查询所有一级类别

    public Collection<Category> getTopAll() {
        JdbcTemplate jdbcTemplate = categoryDao.getJdbcTemplate();
        List<Category> categoryList = jdbcTemplate.query("select id, category_name, parent_id, detail from category where parent_id is null ", new BeanPropertyRowMapper<>(Category.class));
        return categoryList;
    }

    // 查询所有类别
    @DataProvider
    public void getAllCategories(Page<Category> page) {
       categoryDao.getAllCategories(page);
    }
    //

    public Collection<Category> getCategoryById(Long id) {
        JdbcTemplate jdbcTemplate = categoryDao.getJdbcTemplate();
        List<Category> categoryList = jdbcTemplate.query("select id, category_name, parent_id, detail from category where parent_id = ?", new Object[]{id}, new BeanPropertyRowMapper<>(Category.class));
        for (Category category : categoryList) {
            Category category1 = new Category();
            if (category != null && category.getParentId() != null) {
                category1 = jdbcTemplate.queryForObject("select id, category_name, detail from category where id = ?", new Object[]{category.getParentId()}, new BeanPropertyRowMapper<>(Category.class));
            } else {
                category1.setCategoryName("顶级类别");
            }
            category.setParent(category1);
        }
        return categoryList;
    }
    @DataResolver
    @Transactional
    public void save(Collection<Category> categoryList) {
        JdbcTemplate jdbcTemplate = categoryDao.getJdbcTemplate();
        for (Category category : categoryList) {
            saveCategory(jdbcTemplate, category);
            Collection<Category> childCategories = category.getChildCategories();
            if (childCategories != null) {
                save(childCategories);
            }
        }
    }
    public void saveCategory(JdbcTemplate jdbcTemplate ,Category category) {
        if (EntityState.DELETED.equals(EntityUtils.getState(category))) {
            jdbcTemplate.update("delete from category where id = ? ", new Object[]{category.getId()});
        } else if (EntityState.MODIFIED.equals(EntityUtils.getState(category))) {
            jdbcTemplate.update("update category set category_name = ?, parent_id = ?, detail = ? where id = ?", new Object[]{category.getCategoryName(), category.getParentId(), category.getDetail(), category.getId()});
        } else if (EntityState.NEW.equals(EntityUtils.getState(category))) {
            // jdbcTemplate.update("insert into category (category_name,parent_id,detail) values(?,?,?)",category.getCategoryName(),category.getParentId(),category.getDetail());
            categoryJPADao.save(category);
        }
    }
    public List<Category> getChildCategories(Long id) {
        List<Category> categories = categoryJPADao.findCategoriesByParentIdEquals(id);
        return categories;
    }
    public void saveCategory(Category category) {
        categoryJPADao.save(category);
    }


    public void delCategory(Long id) {
        categoryJPADao.deleteById(id);
    }
}
